//
//  LoginDownView.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/17.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  登陆页面下半部分uiview自定义

import UIKit

//代理
protocol LoginDownViewDelegate {
    ///微信代理事件
    func wxloginSEL()
    
    ///QQ代理事件
    func qqLoginSEL()
}


class LoginDownView: UIView {
    var wxImg = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // MARK:- 左边微信登陆视图
        self.leftView()
        // MARK:- 左边微信登陆视图
        self.rightView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置代理监听按钮事件
    var delegate : LoginDownViewDelegate?
}

// MARK:- 左边微信登陆视图
extension LoginDownView {
    func leftView() -> Void {
        //左边微信登陆视图
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: self.bounds.width * 0.5, height: self.bounds.height * 0.5))
        
        self.addSubview(leftView)
        
        //微信按钮
        let btnW = leftView.frame.size.width * 0.3

        
        //分开搞，图片加label
        wxImg = UIImageView.init(frame: CGRect(x: leftView.frame.size.width * 0.3, y: leftView.frame.size.height * 0.5 - btnW * 0.3, width: leftView.Width * 0.3, height: leftView.Width * 0.3))
        wxImg.center = leftView.center
        
        wxImg.image = UIImage.init(named: "wx")
        wxImg.contentMode = .scaleAspectFit
        
        //文本
        let wxDesc = UILabel.init(frame: CGRect(x:0, y: wxImg.BottomY + 10, width: leftView.Width, height: 20))
        wxDesc.font = UIFont.systemFont(ofSize: 14)
        wxDesc.textColor = UIColor.lightGray
        
        
        //打开交互手势
        wxImg.isUserInteractionEnabled = true
        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(LoginDownView.clickWXImgSEL))
        wxImg.addGestureRecognizer(tapGes)
        
        wxDesc.text = "微信登录"
        wxDesc.textAlignment = .center
        
        leftView.addSubview(wxImg)
        leftView.addSubview(wxDesc)

    }
}


// MARK:- 左边微信登陆视图
extension LoginDownView {
    func rightView() -> Void {
        //右边QQ登陆视图
        let rightView = UIView(frame: CGRect(x: SW * 0.5, y: 0, width:self.bounds.width * 0.5, height: self.bounds.height * 0.5))
        
        self.addSubview(rightView)

        //微信按钮
        let btnW = rightView.frame.size.width * 0.3
        
        
        //分开搞，图片加label
        let qqImg = UIImageView.init(frame: CGRect(x: rightView.frame.size.width * 0.5 - rightView.Width * 0.15 , y: rightView.frame.size.height * 0.5 - btnW * 0.3 - btnW * 0.15 , width: rightView.Width * 0.3, height: rightView.Width * 0.3))

        qqImg.image = UIImage.init(named: "qq")
        qqImg.contentMode = .scaleAspectFit
        
        //文本
        let qqDesc = UILabel.init(frame: CGRect(x:0, y: qqImg.BottomY + 10, width: rightView.Width, height: 20))
        qqDesc.font = UIFont.systemFont(ofSize: 14)
        qqDesc.textColor = UIColor.lightGray
        
        
        //打开交互手势
        qqImg.isUserInteractionEnabled = true
        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(LoginDownView.clickQQImgSEL))
        qqImg.addGestureRecognizer(tapGes)
        
        qqDesc.text = "QQ登录"
        qqDesc.textAlignment = .center
        
        rightView.addSubview(qqImg)
        rightView.addSubview(qqDesc)


    }
}

// MARK:- 单机事件
extension LoginDownView {
    //微信
    func clickWXImgSEL() -> Void {

        //XFLog(message: "微信")
        
        
        if WXApi.isWXAppInstalled() == false {
            MBManager.showBriefAlert("未安装微信或版本不支持")
        } else {
            delegate?.wxloginSEL()
        }

    }
    
    //QQ
    func clickQQImgSEL() -> Void {
        //XFLog(message: "QQ")
        
//        if QQApiInterface.isQQInstalled() == false {
//           MBManager.showBriefAlert("未安装QQ或版本不支持")
//        } else {
            delegate?.qqLoginSEL()
//        }
        
        
    }
}




